package com.mq.rocketmq.ordermsg;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: xiaoqiang
 * @Description:
 * @Date: 2024/7/3
 * @Version: 1.0
 */
public class OrderlyConsumerSingleThread {

    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");

        consumer.setNamesrvAddr("localhost:9876");

        consumer.subscribe("orderlyTopic", "*");

        //必须使用单线程监听器
        consumer.setMessageListener(new MessageListenerOrderly() {

            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                list.forEach(messageExt -> {
                    System.out.println("消息内容：" + new String(messageExt.getBody()) + " , 线程：" + Thread.currentThread().getId() + " , 存储队列： " + messageExt.getQueueId());
                });
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });

        consumer.start();
    }
}
